//设置ajax请求结束后的执行动作
$.ajaxSetup({
    complete: function (XMLHttpRequest, textStatus, error) {
        var redirect = XMLHttpRequest.getResponseHeader("REDIRECT");
        if (redirect == "REDIRECT") {
            var win = window;
            while (win != win.top) {
                win = win.top;
            }
            //将后端重定向的地址取出来,使用win.location.href去实现重定向的要求
            layer.msg("登录超时请重新登录！", {icon: 5, time: 1500}, function () {
                win.location.href = XMLHttpRequest.getResponseHeader("CONTEXTPATH");
            })
        }
    },
    type: 'POST'
});

/**
 * 序列化查询条件 form表单
 * @param FormId
 */
function serializeObject(FormId) {
    var o = {};
    var a = $("#" + FormId).serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            if (this.value) {
                o[this.name].push(this.value);
            }
        } else {
            if (this.value) {
                o[this.name] = this.value;
            }
        }
    });
    return o;
}

/**
 * post ajax
 * @param url
 * @param data
 * @param success
 * @param error
 */
function post(url, data, success, error) {
    $.ajax({
        url: url,
        type: 'POST',
        data: data,
        success: function (data) {
            success(data)
        },
        error: function (data) {
            error(data)
        }
    })
}

/**
 * get ajax
 * @param url
 * @param data
 * @param success
 * @param error
 */
function get(url, data, success, error) {
    $.ajax({
        url: url,
        type: 'GET',
        data: data,
        success: function (data) {
            success(data)
        },
        error: function (data) {
            error(data)
        }
    })
}

/**
 * form 表单提交
 * @param url
 * @param data
 * @param success
 * @param error
 */
function form(url, data, success, error) {
    $.ajax({
        url: url,
        type: 'POST',
        data: data,
        contentType: false,
        processData: false,
        success: function (data) {
            success(data)
        },
        error: function (data) {
            error(data)
        }
    })
}


function request(url, method, data, callback, error) {

    if (method == 0) {
        method = 'GET'
    } else if (method == 1) {
        method = 'POST'
    } else {
        method = 'GET'
    }

    $.ajax({
        url: url,
        type: method,
        data: data,
        success: function (data) {
            console.log(data)
            callback(data)
        },
        error: function (data) {
            error(data)
        }
    })
}

function changeDateFormat(fmt, date) {
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

//树形菜单
var zTree;

function initTree(url) {
    var setting = {
        data: {
            simpleData: {
                enable: true, // 设置启用简单数据格式[{id, pId, name}, {id, pId, name}]
                idKey: "id",  // 节点数据中保存唯一标识的属性名称
                pIdKey: "pId",  // 节点数据中保存其父节点唯一标识的属性名称
                rootPId: null  // 根节点id
            }
        }
    };
    request(url, 0, null, function (data) {
        if (data.code == 1) {
            zTree = $.fn.zTree.init($("#typeTree"), setting, data.data);
        } else {
            layer.msg("树表初始化失败！")
        }
    }, function (error) {
        layer.msg("系统异常！")
    })
}

function openTree(id) {
    layer.open({
        type: 1,
        area: '300px',
        title: '选择上级节点',
        shade: 0.6,
        maxmin: false,
        anim: 1,
        btn: ['确定', '取消'],
        content: id,
        success: function (layero, index) {
            layer.iframeAuto(index);
        },
        yes: function (index, layero) {
            doSetSelectedNode(index);
        },
        btn2: function (index) {
            layer.close(index);
        }
    });
}

/*设置选中的节点*/
function doSetSelectedNode(index) {
    var selectedNodes = zTree.getSelectedNodes();
    var node = selectedNodes[0];
    $("#parentName").val(node.name);
    $("#parentId").val(node.id);
    layer.close(index);
}
